package com.zhangxu.microservice.system.dao.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangxu.microservice.system.dao.condition.RoleCondition;
import com.zhangxu.microservice.system.dao.entity.RoleDO;
import com.zhangxu.microservice.system.dao.mapper.RoleMapper;
import com.zhangxu.microservice.system.dao.service.RoleDao;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

/**
 * 角色DAO实现类
 */
@Repository
public class RoleDaoImpl extends ServiceImpl<RoleMapper, RoleDO> implements RoleDao {

    @Override
    public RoleDO getByCode(String code) {
        return this.lambdaQuery()
                .eq(RoleDO::getCode, code)
                .eq(RoleDO::getDeleteTime, 0L)
                .one();
    }

    @Override
    public RoleDO getByAppIdAndCode(Long appId, String code) {
        return this.lambdaQuery()
                .eq(RoleDO::getAppId, appId)
                .eq(RoleDO::getCode, code)
                .eq(RoleDO::getDeleteTime, 0L)
                .one();
    }

    @Override
    public List<RoleDO> listByAppId(Long appId) {
        return this.lambdaQuery()
                .eq(RoleDO::getAppId, appId)
                .eq(RoleDO::getDeleteTime, 0L)
                .list();
    }

    @Override
    public RoleDO getById(Serializable id) {
        return this.lambdaQuery()
                .eq(RoleDO::getId, id)
                .eq(RoleDO::getDeleteTime, 0L)
                .one();
    }

    @Override
    public IPage<RoleDO> page(RoleCondition condition) {
        condition.checkPage();
        return this.lambdaQuery()
                .like(StrUtil.isNotBlank(condition.getName()), RoleDO::getName, condition.getName())
                .eq(StrUtil.isNotBlank(condition.getCode()), RoleDO::getCode, condition.getCode())
                .eq(condition.getStatus() != null, RoleDO::getStatus, condition.getStatus())
                .eq(RoleDO::getDeleteTime, 0L)
                .page(condition.getIpage());
    }
}